#pragma semicolon 1
#include <sourcemod>
#include <cstrike>
#include <sdktools>
#include <sdkhooks>
#include <ncs_rpg>

#define VERSION		"Beta 1.0.2"

new ThisSkillID;

new m_hOwner;

new const String:WeaponNameForSlot[][] = {
								"weapon_galil weapon_ak47 weapon_scout weapon_sg552 weapon_awp weapon_g3sg1 weapon_famas weapon_m4a1 weapon_aug weapon_sg550 weapon_m3 weapon_xm1014 weapon_mac10 weapon_tmp weapon_mp5navy weapon_ump45 weapon_p90 weapon_m249",
								"weapon_glock weapon_usp weapon_p228 weapon_deagle weapon_fiveseven weapon_elite",
								"weapon_knife",
								"weapon_hegrenade weapon_flashbang weapon_smokegrenade",
								"weapon_c4"
};

new Handle:ArrayWeapons[MAXPLAYERS+1] = {INVALID_HANDLE, ...};

public Plugin:myinfo =
{
	name		= "[NCS RPG] Skill Recovery Weapon",
	author		= "[RUS] DiviX",
	description	= "Skill Recovery Weapon for NCS RPG",
	version		= VERSION,
	url			= "N/A"
};

public OnPluginStart()
{
	m_hOwner = FindSendPropOffs("CBaseCombatWeapon", "m_hOwner");
	HookEvent("player_spawn", Event_PlayerSpawn);
}

public RPG_OnSkillsRegister()
{
	ThisSkillID = RegisterSkill("recovery", 3, 15, 5);
}

public OnClientPutInServer(client)
{
	ArrayWeapons[client] = CreateArray(16);
}

public OnClientDisconnect(client)
{
	if(ArrayWeapons[client] == INVALID_HANDLE)	// client cant put in server or ..?
		return;
	
	new weapons = GetArraySize(ArrayWeapons[client]);
	if(weapons > 0)
		ClearArray(ArrayWeapons[client]);
}

public Action:CS_OnCSWeaponDrop(client, weaponIndex)
{
	if(!IsValidPlayer(client) || !IsValidEdict(weaponIndex))
		return;
	
	if(GetClientHealth(client) > 0)
		return;
	
	decl String:weapon[32];
	GetEdictClassname(weaponIndex, weapon, sizeof(weapon));
	if(StrEqual(weapon, "weapon_knife") || StrEqual(weapon, "weapon_c4"))
		return;
	
	PushArrayString(ArrayWeapons[client], weapon);
}

public OnEntityDestroyed(entity)
{
	if(!IsValidEdict(entity))
		return;
	
	decl String:weapon[32];
	GetEdictClassname(entity, weapon, sizeof(weapon));
	if(StrContains(weapon, "weapon_") != -1)
	{
		if(StrEqual(weapon, "weapon_knife") || StrEqual(weapon, "weapon_c4"))
			return;
		
		new client = GetEntDataEnt2(entity, m_hOwner); //GetEntPropEnt(entity, Prop_Data, "m_hOwner"); // m_hOwnerEntity FindSendPropOffs(
		if(!IsValidPlayer(client))
			return;
		
		if(IsPlayerAlive(client))
			return;
		
		PushArrayString(ArrayWeapons[client], weapon);
	}
}

public Action:CS_OnTerminateRound(&Float:delay, &CSRoundEndReason:reason)
{
	CreateTimer(delay-0.1, Timer_RoundEnd, reason, TIMER_FLAG_NO_MAPCHANGE);
}

public Action:Timer_RoundEnd(Handle:timer, any:reason)
{
	decl String:weapon[32];
	new bool:clear = false;
	if(reason == CSRoundEnd_GameStart || reason == CSRoundEnd_Draw)
		clear = true;
	
	for(new client = 1; client <= MaxClients; client++)
	{
		if(ArrayWeapons[client] == INVALID_HANDLE)
			continue;
		
		if(clear)
		{
			new weapons = GetArraySize(ArrayWeapons[client]);
			if(!weapons)
				continue;
			
			ClearArray(ArrayWeapons[client]);
			continue;
		}
		
		if(IsValidPlayer(client, true))
		{
			for(new i = 0; i <= CS_SLOT_GRENADE; i++)
			{
				new entity = GetPlayerWeaponSlot(client, i);
				if(entity != -1)
				{
					GetEdictClassname(entity, weapon, sizeof(weapon));
					if(StrEqual(weapon, "weapon_knife") || StrEqual(weapon, "weapon_c4"))
						continue;
					
					PushArrayString(ArrayWeapons[client], weapon);
				}
			}
		}
	}
}

public Event_PlayerSpawn(Handle:event, const String:name[], bool:dontBroadcast)
{
	new client = GetClientOfUserId(GetEventInt(event, "userid"));
	if(IsValidPlayer(client))
		if(GetClientTeam(client) != CS_TEAM_NONE)
			CreateTimer(0.0, Timer_PostSpawn, client, TIMER_FLAG_NO_MAPCHANGE);
}

public Action:Timer_PostSpawn(Handle:timer, any:client)
{
	if(IsValidPlayer(client, true))
	{
		new level = GetSkillLevel(client, ThisSkillID);
		if(level > 0)
		{
			new weapons = GetArraySize(ArrayWeapons[client]);
			if(!weapons)
				return;
			
			decl String:weapon[32];
			new weaponIndex = -1;
			
			for(new i = 0; i < weapons; i++)
			{
				GetArrayString(ArrayWeapons[client], i, weapon, sizeof(weapon));
				if(StrContains(WeaponNameForSlot[CS_SLOT_PRIMARY], weapon) != -1)
				{
					if(level < 3)
						continue;
					else
					{
						weaponIndex = GetPlayerWeaponSlot(client, CS_SLOT_PRIMARY);
						if(IsValidEdict(weaponIndex))
							RemovePlayerItem(client, weaponIndex);
					}
				}
				else if(StrContains(WeaponNameForSlot[CS_SLOT_SECONDARY], weapon) != -1)
				{
					if(level < 2)
						continue;
					else
					{
						weaponIndex = GetPlayerWeaponSlot(client, CS_SLOT_SECONDARY);
						if(IsValidEdict(weaponIndex))
							RemovePlayerItem(client, weaponIndex);
					}
				}
				
				GivePlayerItem(client, weapon);
			}
		}
		ClearArray(ArrayWeapons[client]);
	}
}